#pragma once

namespace solution_024
{

struct ListNode
{
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution
{
public:
    ListNode* swapPairs(ListNode* head)
    {
        ListNode root(0, head);
        auto prev = &root;
        for (auto node = head; node && node->next; ) {
            auto next = node->next->next;

            prev->next = node->next;
            node->next->next = node;
            node->next = next;

            prev = node;
            node = next;
        }
        return root.next;
    }
};

}  /* namespace solution_024 */
